package com.education.cms.web;

import com.education.cms.common.ResultModle;
import com.education.cms.domain.Course;
import com.education.cms.dto.course.AddCourseDto;
import com.education.cms.dto.course.EditCourseDto;
import com.education.cms.service.CourseService;
import com.education.cms.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private UserUtils userUtils;

    /**
     * 展示选课列表
     */
    @GetMapping("showCourseList")
    public ResultModle showCourseList(){
        List<Course> courses = courseService.getCourseList();
        return ResultModle.ok(courses);
    }
    /**
     * 添加一门选课
     */
    @PostMapping("addCourse")
    public ResultModle addCourse(@RequestBody AddCourseDto addCourseDto){
        return ResultModle.ok(courseService.addCourse(addCourseDto));
    }
    /**
     * 根据courseId得到一门课程(修改页面回显课程原有数据
     */
    @GetMapping("editPage/{courseId}")
    public ResultModle editPage(@PathVariable("courseId")int courseId){
        return ResultModle.ok(courseService.course(courseId));
    }
    /**
     * 修改一门选课
     */
    @PostMapping("editCourse")
    public ResultModle editCourse(@RequestBody EditCourseDto editCourseDto){
        if (editCourseDto.getCourseName().isEmpty() || editCourseDto.getCourseCredit() == null || editCourseDto.getCourseTime() == null || editCourseDto.getCourseMax() == null){
            return ResultModle.error("相关数据不可为空！");
        }else return ResultModle.ok(courseService.editCourse(editCourseDto));
    }
    /**
     * 选课报名(同一时段仅可选一门 学生权限
     */
    @PostMapping("enroll")
    public ResultModle enroll(@RequestParam("courseId")int courseId, HttpServletRequest request){
        int userId = userUtils.getUserId(request);
        return ResultModle.ok(courseService.enroll(courseId,userId));
    }
    /**
     * 结课(管理员权限
     */
    @PostMapping("finishClass")
    public ResultModle enroll(@RequestParam("courseId")int courseId){
        courseService.finishClass(courseId);
        return ResultModle.ok("此期班级已结课！");
    }
}
